package pers.agony.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.agony.dao.CheckItemDao;
import pers.agony.entity.PageResult;
import pers.agony.entity.QueryPageBean;
import pers.agony.entity.Result;
import pers.agony.pojo.CheckItem;
import pers.agony.service.CheckItemService;

import java.util.List;

/**
 * @author : Agony
 * @date : Created in 8:28 下午 2021/10/31
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public int add(CheckItem checkItem) {
        return checkItemDao.add(checkItem);
    }

    @Override
    public PageResult<CheckItem> findPage(QueryPageBean queryPageBean) {
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        //第二种，Mapper接口的调用方式，推荐使用这种方式
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemDao.findByCondition(queryPageBean.getQueryString());

        //获取总页数
        long total = page.getTotal();
        //获取页面list
        List<CheckItem> checkItems = page.getResult();
        return new PageResult<>(total,checkItems);
    }

    @Override
    public int delete(int id) {
        return checkItemDao.delete(id);
    }

    @Override
    public Result findOne(int id) {
        CheckItem checkItem = checkItemDao.findOne(id);
        if(checkItem!=null){
            return new Result(true,"查询成功！",checkItem);
        }else{
            return new Result(true,"查询失败！");
        }
    }

    @Override
    public Result update(CheckItem checkItem) {
        int rows = checkItemDao.update(checkItem);
        if(rows>0){
            return new Result(true,"更新成功！");
        }else{
            return new Result(false,"更新失败！");
        }
    }
}
